package com.caoxw.Android.CityBus;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import android.content.Context;
import android.util.Log;

public final  class FileData {
	private   Context		m_context = null;
	private   byte[]		m_DataBlock;
	private   int			m_intPosition = 0;
	private   int 			m_intFileSize = 0;
	private   String		m_strFilePath;
	public FileData(Context	context,String FilePath){
		m_context = context;
		m_strFilePath = FilePath;

	}
	public void free(){
		m_DataBlock = null;;
	}
	public int readInt(){
		byte[] Byte = read(0,4);
		int[] data =new int[4];
		for(int i=0;i<4;i++)
			data[i] = (Byte[i]<0) ? 256+Byte[i] : Byte[i];
			//Byte[i] = (byte) ((Byte[i]<0) ? 256+Byte[i] : Byte[i]);
		 return data[0] * 256 * 256 * 256 + data[1] * 256 * 256 + data[2] * 256 + data[3];
	}
	public int readString(){
		byte[] Byte = read(0,4);
		int[] data =new int[4];
		for(int i=0;i<4;i++)
			data[i] = (Byte[i]<0) ? 256+Byte[i] : Byte[i];
			//Byte[i] = (byte) ((Byte[i]<0) ? 256+Byte[i] : Byte[i]);
		 return data[0] * 256 * 256 * 256 + data[1] * 256 * 256 + data[2] * 256 + data[3];
	}
	/**
	 * 将数据文件一次性载入到内存
	 * @return
	 */
	public boolean loadData(){
		int byteread = 0;
		int	byteCopyed = 0;
		byte[] buf = new byte[1024]; 
		try {
			FileInputStream fis=  m_context.openFileInput(m_strFilePath);
			m_intFileSize = fis.available();
			m_DataBlock = new byte[m_intFileSize];
			while((byteread = fis.read(buf))!=-1){
				System.arraycopy(buf, 0, m_DataBlock, byteCopyed, byteread);
				byteCopyed += byteread;
			} 
		}catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		} 
		return m_intFileSize==byteCopyed;
	}
	public void setPosition(int Position){
		m_intPosition = Position;
	}
	public int getPosition(){
		return m_intPosition;
	}
	
	public byte[] read(int Offset,int length){
		byte[] reBytes = new byte[length];
		for (int i = Offset; i < length; i++){
			if(m_intPosition>=this.m_intFileSize)  break;
			reBytes[i] = m_DataBlock[m_intPosition++];
		}
		return reBytes;
	}
	
	public byte ReadByte(){
		if(m_intPosition>=this.m_intFileSize)  return 0;
		return m_DataBlock[m_intPosition++];
	}
	public String GetStr(int Position){
		return GetStr(Position,true);
	}
	public String GetStr(int Position,boolean Back)
	{
		if(Position>=this.m_intFileSize) return "";
		byte lowC = 0;
		byte[] arrBytes;
		int ReadCount=0;
		int oldPosition=getPosition();
		int lStartPosition = oldPosition;
		if(Position>=0){
			setPosition(Position);
			lStartPosition = readInt();
			setPosition(lStartPosition);
		}
		
		while (true)
		{
			lowC = ReadByte();
			if (lowC == 0)break;
			if (lowC > 127 || lowC < 0) ReadByte();
		}
		ReadCount=getPosition()-lStartPosition;
		arrBytes=new byte[ReadCount];
		setPosition(lStartPosition);
		arrBytes = read(0,ReadCount);
		String rtStr;
		try {
			rtStr = new String(arrBytes,"gbk");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rtStr = new String(arrBytes);
		}
		if(Back) setPosition(oldPosition);
		return rtStr;
	}

}
